package com.gongJiao.service.impl;

import com.gongJiao.Vo.PageCondition;
import com.gongJiao.entity.Notice;
import com.gongJiao.mapper.NoticeMapper;
import com.gongJiao.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;
    /**
     * 分页查询所有公告信息列表
     * @param pageCondition
     * @return
     */
    @Override
    public List<Notice> getNoticeList(PageCondition pageCondition) {
        return noticeMapper.getNoticeList(pageCondition);
    }

    /**
     * 分页查询所有公告信息数量
     * @param pageCondition
     * @return
     */
    @Override
    public Integer getCount(PageCondition pageCondition) {
        return noticeMapper.getCount(pageCondition);
    }

    /**
     * 根据id获取公告信息
     * @param id
     * @return
     */
    @Override
    public Notice getNoticeById(Long id) {
        return noticeMapper.selectById(id);
    }

    /**
     * 修改公告信息
     * @param notice
     * @return
     */
    @Override
    public Boolean updateNotice(Notice notice) {
        int i = noticeMapper.updateById(notice);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 删除公告信息
     * @param ids
     */
    @Override
    public void deleteNoticeByIds(String ids) {
        String[] idArray = ids.split(",");
        List idList=new ArrayList<>();
        for (String id : idArray) {
            idList.add(id);
        }
        noticeMapper.deleteBatchIds(idList);
    }

    /**
     * 前台根据时间顺序获取所有公告信息
     * @return
     */
    @Override
    public List<Notice> getWebNoticeList() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Notice> noticeList=noticeMapper.getWebNoticeList();
        for (int i = 0; i < noticeList.size(); i++) {
            String dateString = formatter.format(noticeList.get(i).getCreateTime());
            noticeList.get(i).setTime(dateString);
        }
        return noticeList;
    }
}
